package com.gupao.p7store.common.aware;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;


/**
 * desc: 可感知的应用程序属性
 *
 * @ClassName: com.gupao.p7store.common.aware.AwareApplicationProperty
 * @Author: dengbicheng
 * @CreateTime: 2022-02-08 16:43:03
 * @Version: v 1.0.0
 */
@Component
public class AwareApplicationProperty implements EmbeddedValueResolverAware {

    private StringValueResolver resolver;

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
    }

    /**
     * 获取属性值
     *
     * @param key 关键
     * @return {@link String}
     */
    public String getPropertiesValue(String key){
        if(StringUtils.isEmpty(key) || null == this.resolver){
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        String placeholder = buffer.append("${").append(key).append("}").toString();
        return this.resolver.resolveStringValue(placeholder);
    }
}
